<?php


namespace app\admin\model;


use think\Model;

class PayRecords extends Model
{
	protected $createTime = "records_time";
	protected $rate_rate = 0.37;
	protected $rate_close_rate = 0.5;
	public function getRecordsStateAttr($v){
		$txt = '';
		//1 交易成功 2支付失败 3支付中 4代付中 5代付失败
		switch ($v){
			case "1":
				$txt = '交易成功';
				break;
			case "2":
				$txt = '支付失败';
				break;
			case "3":
				$txt = '支付中';
				break;
			case "4":
				$txt = '代付中';
				break;
			case "5":
				$txt = '代付失败';
				break;
		}
		return $txt;
	}
	//统计总的交易金额
	public function totalMoney(){
		$total=0;
		$where = [];
		$where[] = ["records_state","=","1"];
		$query_date = input('query_date', '', 'trim,htmlspecialchars');
		if (!empty($query_date)) {
			$query_date = explode(" - ", $query_date);
			$query_date[0] = strtotime($query_date[0]);
			$query_date[1] = strtotime($query_date[1] . "+1day");
			$where[] = ['records_time', 'between', $query_date];
		}
		$total = $this->where($where)->sum('records_money');
		return $total;
	}
	//统计总的交易费率
	public function totalFee(){
		$total=0;
		$where = [];
		$where[] = ["records_state","=","1"];
		$query_date = input('query_date', '', 'trim,htmlspecialchars');
		if (!empty($query_date)) {
			$query_date = explode(" - ", $query_date);
			$query_date[0] = strtotime($query_date[0]);
			$query_date[1] = strtotime($query_date[1] . "+1day");
			$where[] = ['records_time', 'between', $query_date];
		}
		$total = $this->where($where)->sum('records_fee');
		return $total;
	}
	//统计总的交易费率利润
	public function totalFeeProfit(){
		$total=$this->totalFee();
		$res = round($total*(1-$this->rate_rate),2);
		return $res;
	}
	//统计总的交易代付费
	public function totaldfFee(){
		$total=0;
		$where = [];
		$where[] = ["records_state","=","1"];
		$query_date = input('query_date', '', 'trim,htmlspecialchars');
		if (!empty($query_date)) {
			$query_date = explode(" - ", $query_date);
			$query_date[0] = strtotime($query_date[0]);
			$query_date[1] = strtotime($query_date[1] . "+1day");
			$where[] = ['records_time', 'between', $query_date];
		}
		$total = $this->where($where)->sum('records_close_rate');
		return $total;
	}
	//统计总的交易代付费利润
	public function totaldfFeeProfit(){
		$total=$this->totaldfFee();
		$where = [];
		$where[] = ["records_state","=","1"];
		$query_date = input('query_date', '', 'trim,htmlspecialchars');
		if (!empty($query_date)) {
			$query_date = explode(" - ", $query_date);
			$query_date[0] = strtotime($query_date[0]);
			$query_date[1] = strtotime($query_date[1] . "+1day");
			$where[] = ['records_time', 'between', $query_date];
		}
		$count = $this->where($where)->count('records_id');
		$res = $total-round($count*$this->rate_close_rate,2);
		return $res;
	}
}